package org.springblade.modules.system.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.cache.DictCache;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.RescueScreenConfig;
import org.springblade.modules.repair.entity.ZgRedeployConfig;
import org.springblade.modules.system.dto.ReportConfig;
import org.springblade.modules.system.entity.Dict;
import org.springblade.modules.system.entity.TenantConfig;
import org.springblade.modules.system.mapper.TenantConfigMapper;
import org.springblade.modules.system.service.TenantConfigService;
import org.springblade.modules.system.vo.config.AlarmConfig;
import org.springblade.modules.system.vo.config.AlarmLabelConfig;
import org.springblade.modules.system.vo.config.AlarmNotifyConfig;
import org.springblade.modules.system.vo.config.RescueSignConfig;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TenantConfigServiceImpl extends ServiceImpl<TenantConfigMapper, TenantConfig> implements TenantConfigService{

	private static final String TENANT_CONFIG = "tenant_config";

	@Override
	public TenantConfig getTenantConfig(String tenantId) {

		log.info("================================= TOKEN_TENANT : {} && PARAM_TENANT : {} ===============================", AuthUtil.getTenantId(),tenantId);

		TenantConfig tenantConfig = baseMapper.selectByTenantId(tenantId);
		List<Dict> dictList = DictCache.getList(TENANT_CONFIG);
		if(ObjectUtil.isEmpty(tenantConfig)){
			tenantConfig = new TenantConfig();
		}
		Map<String,Object> configMap = tenantConfig.getConfig();
		if(ObjectUtil.isEmpty(configMap)){
			configMap = new HashMap<>();
		}
		Map<String, Object> finalConfigMap = configMap;
		dictList.forEach(e -> {
			if(!finalConfigMap.containsKey(e.getDictKey())){
				finalConfigMap.put(e.getDictKey(),e.getDictValue());
			}
		});
		RescueScreenConfig rescueScreenConfig = tenantConfig.getRescueScreenConfig();
		if (Func.isNull(rescueScreenConfig)){
			rescueScreenConfig = new RescueScreenConfig();
			tenantConfig.setRescueScreenConfig(rescueScreenConfig);
		}
		AlarmLabelConfig alarmLabelConfig = tenantConfig.getAlarmLabelConfig();
		if (Func.isNull(alarmLabelConfig)){
			alarmLabelConfig = new AlarmLabelConfig();
			tenantConfig.setAlarmLabelConfig(alarmLabelConfig);
		}
		if(Func.isEmpty(tenantConfig.getReportConfig())){
			ReportConfig reportConfig = new ReportConfig();
			reportConfig.setWxOpenOrNot(1);
			reportConfig.setWxNumType(1);
			tenantConfig.setReportConfig(reportConfig);
		}
		if (Func.isEmpty(tenantConfig.getZgRedeployConfig())){
			ZgRedeployConfig zgRedeployConfig = new ZgRedeployConfig();
			zgRedeployConfig.setRedeployDays(7);
			zgRedeployConfig.setApproverUsers(new ArrayList<>());
			zgRedeployConfig.setUserAndRoles(new ArrayList<>());
			tenantConfig.setZgRedeployConfig(zgRedeployConfig);
		}
		if (Func.isEmpty(tenantConfig.getAlarmNotifyConfig())) {
			tenantConfig.setAlarmNotifyConfig(new AlarmNotifyConfig());
		}
		if (Func.isEmpty(tenantConfig.getAlarmConfig())) {
			tenantConfig.setAlarmConfig(new AlarmConfig());
		}
		if (Func.isEmpty(tenantConfig.getRescueSignConfig())) {
			tenantConfig.setRescueSignConfig(new RescueSignConfig());
		}
		// 将 finalConfigMap 与 rescueScreenConfig 合并
		finalConfigMap.putAll(Func.toMap(rescueScreenConfig));
		finalConfigMap.putAll(Func.toMap(alarmLabelConfig));
		tenantConfig.setConfig(finalConfigMap);
		tenantConfig.setTenantId(tenantId);
		tenantConfig.setCreateTime(DateUtil.date());
		tenantConfig.setUpdateTime(DateUtil.date());
		super.saveOrUpdate(tenantConfig);
		return tenantConfig;
	}

}
